/**
 * 
 */
package it.unibo.refolding.common;

import java.io.Serializable;

import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

public class DoubleIntPair implements Comparable<DoubleIntPair>, Serializable {

	private static final long serialVersionUID = -5166295504157898134L;

	public DoubleIntPair(double x, int y) {
		this.x = x;
		this.y = y;
	}

	public int compareTo(DoubleIntPair other) {
		int cmp = Double.compare(x, other.x);
		if (cmp == 0) {
			cmp = y < other.y ? -1 : (y > other.y ? +1 : 0);
		}
		return cmp;
	}

	public final double x;
	public final int y;

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(x).append(y).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof DoubleIntPair)) {
			return false;
		}
		DoubleIntPair other = (DoubleIntPair) obj;
		return Double.doubleToLongBits(x) == Double.doubleToLongBits(other.x) && y == other.y;
	}

	public DoubleIntPair withX(int someX) {
		return new DoubleIntPair(someX, y);
	}

	public static double[] x(DoubleIntPair[] timeAndSlopeIndex) {
		double[] a = new double[timeAndSlopeIndex.length];
		for (int i = a.length; --i >= 0;) {
			a[i] = timeAndSlopeIndex[i].x;
		}
		return a;
	}

  public static int[] y(DoubleIntPair[] timeAndSlopeIndex) {
    int[] a = new int[timeAndSlopeIndex.length];
    for (int i = a.length; --i >= 0;) {
      a[i] = timeAndSlopeIndex[i].y;
    }
    return a;
  }

	public static DoubleIntPair of(double x, int y) {
		return new DoubleIntPair(x, y);
	}
}
